﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using RetailManagement.Models;

namespace RetailManagement.Controllers
{
    public class CustomerController : Controller
    {
        RetailManagementEntities re = new RetailManagementEntities();
        public static int CustomerID_Order;
        //
        // GET: /Customer/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult LogOff()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("List_Voucher", "Voucher");
        }

        public string getCusPassword(string email)
        {
            var cus = from o in re.Customers where o.Email == email select o;
            CustomerID_Order = cus.First().Customer_ID;
            if (cus.ToList().Count > 0)
                return (cus.First().Password);
            else
                return string.Empty;
        }

        public ActionResult Login()
        {
            return View();
        }


        [HttpPost]
        public ActionResult Login(Logon log, string returnUrl)
        {

            if (ModelState.IsValid)
            {
                
                string password = getCusPassword(log.Email);
                if (string.IsNullOrEmpty(password))
                {
                    ModelState.AddModelError("", "Your Email has already used");
                }
                if (log.Password == password)
                {

                    FormsAuthentication.SetAuthCookie(log.Email, false);
                    Session["Customer_Email"] = log.Email;
                    if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                    && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("List_Voucher", "Voucher");
                    }

                }
                else
                {
                    ModelState.AddModelError("", "The password provided is incorrect.");
                }
            }
            return View(log);
        }
        public ActionResult SignUp()
        {
            return View();
        }

        [HttpPost]
        public ActionResult SignUp(CustomerDB cus)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    CustomerManager cusmanager = new CustomerManager();
                    if (!cusmanager.IsCustomerIDExist(cus.Email))
                    {
                        cusmanager.Add(cus);
                        FormsAuthentication.SetAuthCookie(cus.Email, false);
                        Session["Customer_Email"] = cus.Email;
                        return RedirectToAction("List_Voucher", "Voucher");
                    }
                    else
                    {
                        ModelState.AddModelError("", "Your Email has already used");
                    }
                }
            }
            catch
            {
                return View(cus);
            }
            return View(cus);
        }

    }
}
